package com.itheima.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.itheima.bean.Student;
import com.itheima.service.StudentService;
import com.itheima.service.impl.StudentServiceImpl;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.converters.DateConverter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@WebServlet("/studentServlet")
public class StudentController extends HttpServlet {
    StudentService  studentService = new StudentServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        //获取方法名，通过方法名进行增删改查的功能
        String method = req.getParameter("method");
        if(method.equals("selectByPage")){
            selectByPage(req,resp);
        }else if (method.equals("addStu")){
            addStu(req,resp);
        }else if (method.equals("deleteStu")){
            deleteStu(req,resp);
        }else if (method.equals("updateStu")){
            updateStu(req,resp);
        }

    }

    private void updateStu(HttpServletRequest req, HttpServletResponse resp) {
        Map<String, String[]> map = req.getParameterMap();
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        Student stu = new Student();
        dateConvert();
        try {
            BeanUtils.populate(stu,map);

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        studentService.updateStu(stu);

        try {
            resp.sendRedirect(req.getContextPath()+"/studentServlet?method=selectByPage&currentPage="+currentPage+"pageSize="+pageSize);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void deleteStu(HttpServletRequest req, HttpServletResponse resp) {
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        String number = req.getParameter("number");

       studentService.deleteStu(number);

        try {
            resp.sendRedirect(req.getContextPath()+"/studentServlet?method=selectByPage&currentPage="+currentPage+"pageSize="+pageSize);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //添加
    private void addStu(HttpServletRequest req, HttpServletResponse resp) {
        Map<String, String[]> map = req.getParameterMap();
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        Student stu = new Student();
        dateConvert();
        try {
            BeanUtils.populate(stu,map);

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        studentService.addStu(stu);

        try {
            resp.sendRedirect(req.getContextPath()+"/studentServlet?method=selectByPage&currentPage="+currentPage+"pageSize="+pageSize);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //查询
    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) {
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");

        Page page = studentService.selectStudent(Integer.parseInt(currentPage), Integer.parseInt(pageSize));

        PageInfo pageInfo = new PageInfo(page);

        try {
            String json  = new ObjectMapper().writeValueAsString(pageInfo);
            resp.getWriter().write(json);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    private  void dateConvert(){
        ConvertUtils.register(new Converter() {
            public Object convert (Class type ,Object value) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    return simpleDateFormat.parse(value.toString());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                return null;
            }
            }, Date.class);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
